package qmsz.api.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import qmsz.api.domain.*;
import qmsz.api.service.*;
import qmsz.api.utils.*;

@RestController
@CrossOrigin
public class TempController {
	
//	@Autowired
//	private QMAuthService qmauthService;
	
	@Autowired
	private QMBannerService qmbannerService;
	
//	@Autowired
//	private QMCardService qmcardService;
	
//	@Autowired
//	private QMChargeOrderService qmcoService;
	
//	@Autowired
//	private QMChargeOrderDetailService qmcodService;
	
	@Autowired
	private QMChargeOrderLocationService qmcolService;
	
	@Autowired
	private QMDeliverInfoService qmdiService;
	
//	@Autowired
//	private QMItemService qmitemService;
	
	@Autowired
	private QMProvCityDistService qmpcdService;
	
	@Autowired
	private QMUserService qmuserService;

	@Autowired
	private SMSService smsService;
	
//	@Autowired
//	private WYZSPlatformService wyszplatformService;

	@RequestMapping(value="/sendSMS", method=RequestMethod.POST)
	public JSONObject sendSMS(@RequestBody JSONObject params) {
		Long phoneNumber = params.getLong("phone");
		if (phoneNumber == null) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM);
		}
		
		int type = smsService.sendSMS(phoneNumber);
		if (type == 4) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_ABUSE, "此手机号今日已达操作次数上限");
		} else if (type == 3) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "短信生成出错");
		} else if (type == 2) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_NESTED_REQUEST, "短信发送出错");
		} else if (type == 1) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_UPDATE, "短信写入出错");
		} else {
			return QMSZUtils.simpleResponse(ResponseCode.SUCCESS, "请求成功，验证码已发送");
		}
	}

	@RequestMapping(value="/findBannerOfType", method=RequestMethod.POST)
	public JSONObject findBannerOfType(@RequestBody JSONObject params) {
		Integer type = params.getInteger("type");
		if (type == null) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM);
		}
		
		QMBanner banner = qmbannerService.findBannerOfType(type);
		return QMSZUtils.queryResponse(banner);
	}

//	@RequestMapping(value="/checkCardNo", method=RequestMethod.POST)
//	public JSONObject checkCardNo(@RequestBody JSONObject params) {
//		Long phoneNumber = params.getLong("phone");
//		String smsCode = params.getString("sms");
//		String cardNo = params.getString("cardNo");
//		
//		if (phoneNumber == null || smsCode == null || cardNo == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM);
//		} else if (!smsService.checkSMS(phoneNumber, smsCode)) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM, "验证码错误或已过期");
//		} else {
//			qmuserService.insertUserByMobile(phoneNumber);
//		}
//		
//		QMChargeOrderDetail cod = qmcodService.findChargeOrderDetailByCardNum(cardNo);
//		if (cod == null || cod.getStatus() != 0) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "卡号不存在或已使用");
//		}
//		
//		QMChargeOrder co = qmcoService.findChargeOrderByOrderNum(cod.getOrderNum());
//		if (co == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "订单信息查找失败");
//		}
//		
//		QMItem item = qmitemService.findItemByCode(co.getGoodsCode());
//		if (item == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "商品信息查找失败");
//		}
//		
//		JSONObject resp = QMSZUtils.simpleResponse(ResponseCode.SUCCESS);
//		
//		JSONObject respCard = new JSONObject();
//		respCard.put("name", co.getGoodsName());
//		respCard.put("expiryDate", cod.getExpiryDate());
//		respCard.put("cardNum", cod.getCardNum());
//		respCard.put("orderNum", cod.getOrderNum());
//		
//		JSONObject respItem = new JSONObject();
//		respItem.put("name", item.getName());
//		respItem.put("imgUrl", item.getImgUrl());
//		respItem.put("type", item.getType());
//		
//		resp.put("card", respCard);
//		resp.put("item", respItem);
//		
//		return resp;
//	}

	@RequestMapping(value="/listAvailableStores", method=RequestMethod.POST)
	public JSONObject listAvailableStores(@RequestBody JSONObject params) {
		List<QMChargeOrderLocation> results = qmcolService.selectAllLocations();
		return QMSZUtils.queryResponse(results);
	}

//	@RequestMapping(value="/confirmRedemption", method=RequestMethod.POST)
//	public JSONObject confirmRedemption(@RequestBody JSONObject params) {
//		String cardNo = params.getString("cardNo");
//		String cardPwd = params.getString("cardPwd");
//		String account = params.getString("account");
//		
//		if (cardNo == null || cardPwd == null || account == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM);
//		}
//		
//		QMChargeOrderDetail cod = qmcodService.findChargeOrderDetailByCardNum(cardNo);
//		if (cod == null || cod.getStatus() > 0) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "卡号不存在或已使用");
//		}
//		
//		String acc = cod.getAccount();
//		if (!account.equals(acc)) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM, "账号不匹配");
//		}
//		
//		String agn = cod.getAgencyCode();
//		String apk = qmauthService.getAppkeyFromAgencyCode(agn);
//		if (apk == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "应用不存在");
//		}
//		
//		String encryptedPwd = QMSZUtils.WYSZEncryptCardPwd(cardPwd, apk);
//		if (encryptedPwd == null || !encryptedPwd.equals(cod.getCardPwd())) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM, "卡密校验失败");
//		}
//		
//		JSONObject result = wyszplatformService.chargev1(cardNo, encryptedPwd, cod.getOrderNum(), account, agn, apk);
//		if (result == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_NESTED_REQUEST, "远程服务器错误");
//		} else if (result.getInteger("code") != 0) {
//			qmcodService.markUsed(cardNo);
//		}
//		
//		JSONObject resp = QMSZUtils.simpleResponse(ResponseCode.SUCCESS);
//		resp.put("result", result);
//		return resp;
//	}
	
	@RequestMapping(value="/listProvince", method=RequestMethod.POST)
	public JSONObject listProvince(@RequestBody JSONObject params) {
		List<QMProvince> results = qmpcdService.listProvince();
		return QMSZUtils.queryResponse(results);
	}
	
	@RequestMapping(value="/listCityByProvID", method=RequestMethod.POST)
	public JSONObject listCityByProvID(@RequestBody JSONObject params) {
		Integer id = params.getInteger("id");
		if (id == null) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM);
		}

		List<QMCity> results = qmpcdService.listCityByProvID(id);
		return QMSZUtils.queryResponse(results);
	}
	
	@RequestMapping(value="/listDistByCityID", method=RequestMethod.POST)
	public JSONObject listDistByCityID(@RequestBody JSONObject params) {
		Integer id = params.getInteger("id");
		if (id == null) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM);
		}

		List<QMDistrict> results = qmpcdService.listDistByCityID(id);
		return QMSZUtils.queryResponse(results);
	}
	
	@RequestMapping(value="/listUserDeliverInfoByAccount", method=RequestMethod.POST)
	public JSONObject listUserDeliverInfoByAccount(@RequestBody JSONObject params) {
		String account = params.getString("account");
		if (account == null) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM);
		}
		
		String id = qmuserService.getUserIDByAccount(account);
		if (id == null) {
			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "用户不存在");
		}
		
		List<QMUserDeliverInfo> results = qmdiService.listUserDeliverInfoByUserID(id);
		return QMSZUtils.queryResponse(results);
	}
	
//	@RequestMapping(value="/insertUserDeliverInfo", method=RequestMethod.POST)
//	public JSONObject insertUserDeliverInfo(@RequestBody QMUserDeliverInfo udi) {
//		return null;
//	}
	
//	@RequestMapping(value="/updateUserDeliverInfo", method=RequestMethod.POST)
//	public JSONObject updateUserDeliverInfo(@RequestBody QMUserDeliverInfo udi) {
//		// TODO
//		return null;
//	}
	
//	@RequestMapping(value="/listChargeOrderDeliverInfoByAccount", method=RequestMethod.POST)
//	public JSONObject listChargeOrderDeliverInfoByAccount(@RequestBody JSONObject params) {
//		String account = params.getString("account");
//		if (account == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM);
//		}
//		
//		String id = qmuserService.getUserIDByAccount(account);
//		if (id == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "用户不存在");
//		}
//		
//		List<QMChargeOrderDeliverInfo> results = qmdiService.listChargeOrderDeliverInfoByUserID(id);
//		return QMSZUtils.queryResponse(results);
//	}
	
//	@RequestMapping(value="/insertChargeOrderDeliverInfo", method=RequestMethod.POST)
//	public JSONObject insertChargeOrderDeliverInfo(@RequestBody JSONObject params) {
//		return null;
//	}
	
//	@RequestMapping(value="/insertDeliverInfo", method=RequestMethod.POST)
//	public JSONObject insertDeliverInfo(@RequestBody JSONObject params) {
//		String account = params.getString("account");
//		String orderNum = params.getString("orderNum");
//		String phone = params.getString("phone");
//		String city = params.getString("city");
//		String pro = params.getString("pro");
//		String area = params.getString("area");
//		String address = params.getString("address");
//		String contact = params.getString("contact");
//		String zipCode = params.getString("zipCode");
//		if (account == null || orderNum == null || phone == null || city == null || pro == null || area == null ||
//				address == null || contact == null || zipCode == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_PARAM);
//		}
//		
//		String userId = qmuserService.getUserIDByAccount(account);
//		if (userId == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "用户不存在");
//		}
//		
//		String orderId = qmcoService.getChargeOrderIDByOrderNum(orderNum);
//		if (orderId == null) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_SEARCH, "订单不存在");
//		}
//		
//		QMUserDeliverInfo udi = new QMUserDeliverInfo();
//		udi.setUserId(userId);
//		udi.setOrderId(orderId);
//		udi.setMobile(phone);
//		udi.setCity(city);
//		udi.setPro(pro);
//		udi.setArea(area);
//		udi.setAddress(address);
//		udi.setContact(contact);
//		udi.setZipCode(zipCode);
//		
//		if (!qmdiService.insertUserDeliverInfo(udi)) {
//			return QMSZUtils.simpleResponse(ResponseCode.FAIL_SQL_INSERT, "地址信息提交失败");
//		}
//		
//		return null;
//	}
}
